package com.verisette.ncos.service.card.bizunit;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.bean.LabelValueBean;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;
import com.verisette.ncos.web.utilities.SendNotifyMailThread;

public class NotifyRejectByEmailBU extends CommonBusinessUnit {
	private Logger logger = Logger.getLogger(NotifyRejectByEmailBU.class);
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
// TODO Auto-generated method stub
		
		MasterUserProfile sender = (MasterUserProfile)processContext.getUserBean();
		
		TransCustomerOrder order = (TransCustomerOrder)processContext.getValueObject();
		SendNotifyMailThread sendMailThread = new SendNotifyMailThread();
		sendMailThread.setMailTemplate("mail-approve-rejected");
		sendMailThread.setOrder(order);
		sendMailThread.setSender(sender);
		sendMailThread.setSubjectContent(" ถูกปฏิเสธ");
		
		MasterUserProfile receiver = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getCreatedUspId());
		sendMailThread.setReceiver(receiver);
		
		
		//param 1
		StringBuilder sbf = new StringBuilder(receiver.getFirstName());
		sbf.append(" ").append(receiver.getLastName());
		
		if(order.getApproved02UspId()!=null){
			if(logger.isDebugEnabled()){
				logger.debug("Reject level #2");
			}
			
			//send reject to creator
			sendMailThread.addEmailToReceivers(receiver.getUspEmail());
			
			MasterUserProfile approver = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getApprovedUspId());
			sendMailThread.addEmailToReceivers(approver.getUspEmail());
			
			StringBuilder sb = new StringBuilder(" และคุณ ");
			sb.append(approver.getFirstName()).append(" ").append(approver.getLastName());
			sbf.append(sb.toString());
		}
		sendMailThread.addContentParam(sbf.toString());

		//param 2
		sbf = new StringBuilder(sender.getFirstName());
		sbf.append(" ").append(sender.getLastName());
		sendMailThread.addContentParam(sbf.toString());

		//param 3 init
		MasterMember member = order.getMasterMember();
		String requesterName = member.getMmbFnameTh(), requesterLname = member.getMmbLnameTh();
		if( StringUtils.isBlank(requesterName)){
			requesterName = member.getMmbFnameEn();
			requesterLname = member.getMmbLnameEn();
		}

		//param 3 use
		sbf = new StringBuilder(requesterName);
		sbf.append(" ").append(requesterLname);
		sendMailThread.addContentParam(sbf.toString());
		
		//param 4
		sendMailThread.addContentParam(((LabelValueBean)processContext.getOriginalRequest().getValueObject()).getValue());
		
		sendMailThread.start();
	}
}